<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\Unit as Units;
/**
 * 单位接口
 */
class Unit extends Common
{
    protected $noNeedRight = 'getDetail';

    /**
     * 获取单位
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $name 单位名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?config('app_limit'):$limit;

            $name = $this->request->post('name');
            $where = [];
            if(!empty($name)){
                $where['name'] = ['like','%'.$name.'%'];
            }

            $result = Units::getList($where,$page,$limit);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取单位详情
     * @ApiMethod (POST)
     * @param int $id 单位ID
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $result = Units::getDetail($id);
            $this->success('查询成功',$result,200);
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加单位
     * @ApiMethod (POST)
     * @param array $detail 单位数据
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $name = html_entity_decode($this->request->post('name'));
            $detail = [
                'name' => $name,
            ];
            $ret = Units::addDetail($detail);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 更新单位
     * @ApiMethod (POST)
     * @param array $detail 单位数据
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $id = html_entity_decode($this->request->post('id'));
            $name = html_entity_decode($this->request->post('name'));
            $detail = [
                'id'        => $id,
                'name' => $name,
            ];
            $ret = Units::editDetail($detail);
            if($ret['status']==200){
                $this->success('更新成功','',200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除单位
     * @ApiMethod (POST)
     * @param int $id 单位id
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $ret = Units::delDetail($id);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

}
